import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

# 示例数据
data = {
    'x1': [1, 2, 3, 4, 5],
    'x2': [2, 3, 4, 5, 6],
    'y': [3, 5, 7, 9, 11]
}
df = pd.DataFrame(data)

# 分离自变量和因变量
X = df[['x1', 'x2']]
y = df['y']

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 创建多元线性回归模型
model = LinearRegression()
model.fit(X_train, y_train)

# 预测测试集
y_pred = model.predict(X_test)

# 评估模型
mse = mean_squared_error(y_test, y_pred)
print(f"均方误差 (MSE): {mse:.2f}")
print(f"回归方程为: y = {model.intercept_:.2f} + {model.coef_[0]:.2f} * x1 + {model.coef_[1]:.2f} * x2")